var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		banklist:['工商银行','农业银行','中国银行','建设银行','招商银行','邮储银行','交通银行','浦发银行','民生银行','兴业银行','平安银行','中信银行','华夏银行','广发银行','光大银行','北京银行','宁波银行'],
		bankname:'',
		sysset: [],
		userinfo: [],
		aglevelList: [],
		aglevelCount: 0,
		applytj_reach: 0,
		errmsg: '',
		isload:0,
	},
	_onLoad: function (options) {
		var that = this
		wx.setNavigationBarTitle({
			title: (that.data.textset['会员'] || '会员')+'升级'
		});
		$.get('my/levelup', {}, function (res) {
			if (res.status == 0) {
				that.setData({ errmsg: res.msg })
				//$.alert(data.msg);
			} else if (res.status == 2) {
				that.setData({ errmsg: res.msg })
				setTimeout(function () {
					$.goto('index');
				}, 1000)
			} else {
				that.setData({ isload:1,userinfo: res.userinfo,bankname:res.userinfo.bankname,userlevel: res.userlevel,selectedLevel:res.userlevel, sysset: res.sysset, aglevelList: res.aglevelList, aglevelCount: res.aglevelList.length,explain:res.userlevel.explain});
				//if (data.aglevelList.length == 1) {
				//	that.changelevel({ detail: { value: data.aglevelList[0]['id'] } });
				//}
			}
		});
	},
	onPullDownRefresh: function () {
		this.onLoad();
		wx.stopPullDownRefresh()
	},
	cannotapply: function () {
		$.alert('不满足申请条件')
	},
	bindBanknameChange:function(e){
		this.setData({
			bankname: this.data.banklist[e.detail.value]
		})
	},
	formSubmit: function (e) {
		var formdata = e.detail.value
		formdata.bankname = this.data.bankname
		//if (formdata.realname == '' || formdata.tel == '') {
		//	$.alert('请填写姓名和手机号'); return;
		//}
		//if (this.data.aglevelCount == 1) formdata.aglevel = this.data.aglevelList[0].id;
		if (formdata.levelid == '') {
			$.alert('请选择等级'); return;
		}
		$.post('my/levelup',formdata, function (res) {
			if (res.status == 0) {
				$.alert(res.msg);
				return;
			}
			$.success(res.msg);
				setTimeout(function () {
					$.goto(res.url);
				}, 1000)
		},'提交中');
	},
	changelevel: function (e) {
		var levelid = e.detail.value;
		var aglevelList = this.data.aglevelList;
		var agleveldata;
		for (var i in aglevelList) {
			if (aglevelList[i].id == levelid) {
				agleveldata = aglevelList[i];
				break;
			}
		}
		var applytj = [];
		var applytj_reach = 0;
		var member = this.data.userinfo
		if(agleveldata.can_apply==1){
			if (!agleveldata.apply_ordercount) agleveldata.apply_ordercount = 0;
			if (!agleveldata.apply_ordermoney) agleveldata.apply_ordermoney = 0;
			if (!agleveldata.apply_czmoney) agleveldata.apply_czmoney = 0;

			if (parseInt(agleveldata.apply_ordercount) <= 0 && parseFloat(agleveldata.apply_ordermoney) <= 0 && parseInt(agleveldata.apply_rechargemoney) <= 0) {
				applytj_reach = 1;
			}
			if (parseInt(agleveldata.apply_ordercount) > 0) {
				applytj.push('累计订单数量满' + agleveldata.apply_ordercount + '个');
				if (parseInt(member.ordercount) >= parseInt(agleveldata.apply_ordercount)) {
					applytj_reach = 1;
				}
			}
			if (parseFloat(agleveldata.apply_ordermoney) > 0) {
				applytj.push('累计订单金额满' + agleveldata.apply_ordermoney + '元');
				if (parseFloat(member.ordermoney) >= parseFloat(agleveldata.apply_ordermoney)) {
					applytj_reach = 1;
				}
			}
			if (parseFloat(agleveldata.apply_rechargemoney) > 0) {
				applytj.push('累计充值金额满' + agleveldata.apply_rechargemoney + '元');
				if (parseFloat(member.rechargemoney) >= parseFloat(agleveldata.apply_rechargemoney)) {
					applytj_reach = 1;
				}
			}
		}
		var applytj_info = applytj.join(' 或 ');
		var autouptj = [];
		if(agleveldata.can_up==1){
			if(parseFloat(agleveldata.up_wxpaymoney)){
				autouptj.push('微信支付金额满' + agleveldata.up_wxpaymoney + '元');
			}
			if(parseFloat(agleveldata.up_ordermoney)){
				autouptj.push('订单金额满' + agleveldata.up_ordermoney + '元');
			}
			if(parseFloat(agleveldata.up_rechargemoney)){
				autouptj.push('充值金额满' + agleveldata.up_rechargemoney + '元');
			}
			if(parseFloat(agleveldata.up_fxordermoney)){
				autouptj.push('下级总消费金额满' + agleveldata.up_fxordermoney + '元');
			}
			if(parseInt(agleveldata.up_fxdowncount)){
				autouptj.push('下级总人数满' + agleveldata.up_fxdowncount + '人');
			}
			if(agleveldata.up_proid){
				autouptj.push('购买商品['+agleveldata.up_proname+']');
			}
			if(agleveldata.up_getmembercard==1){
				autouptj.push('领取微信会员卡');
			}
		}
		var autouptj_info = autouptj.join(' 或 ');
		this.setData({ applytj_info: applytj_info, applytj_reach: applytj_reach,autouptj_info:autouptj_info,selectedLevel:agleveldata,explain:agleveldata.explain});
	}
}))